package com.xuanzheng.company.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/xuanzheng_company?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8&allowPublicKeyRetrieval=true";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "07568187168";
    private static final int MAX_RETRY_COUNT = 3;
    private static final int RETRY_DELAY_MS = 1000; // 1秒
    
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.err.println("MySQL驱动加载失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static Connection getConnection() throws SQLException {
        SQLException lastException = null;
        
        // 尝试多次连接数据库
        for (int attempt = 1; attempt <= MAX_RETRY_COUNT; attempt++) {
            try {
                Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
                
                // 测试连接是否有效
                if (conn.isValid(5)) { // 5秒超时
                    return conn;
                } else {
                    System.err.println("获取到的数据库连接无效，尝试关闭并重新连接");
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        System.err.println("关闭无效连接失败: " + e.getMessage());
                    }
                    throw new SQLException("数据库连接无效");
                }
            } catch (SQLException e) {
                lastException = e;
                System.err.println("数据库连接失败 (尝试 " + attempt + " / " + MAX_RETRY_COUNT + "): " + e.getMessage());
                
                // 如果不是最后一次尝试，则等待一段时间后重试
                if (attempt < MAX_RETRY_COUNT) {
                    try {
                        Thread.sleep(RETRY_DELAY_MS);
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                        System.err.println("等待重试过程被中断: " + ie.getMessage());
                    }
                }
            }
        }
        
        // 如果所有尝试都失败，抛出最后一个异常
        System.err.println("所有数据库连接尝试均失败，放弃连接");
        throw lastException;
    }
    
    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                System.err.println("关闭ResultSet失败: " + e.getMessage());
                e.printStackTrace();
            }
        }
        
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                System.err.println("关闭Statement失败: " + e.getMessage());
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                if (!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                System.err.println("关闭数据库连接失败: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
    
    // 测试数据库连接是否可用
    public static boolean testConnection() {
        Connection conn = null;
        try {
            conn = getConnection();
            return conn != null && conn.isValid(5); // 5秒超时
        } catch (SQLException e) {
            System.err.println("测试数据库连接失败: " + e.getMessage());
            return false;
        } finally {
            close(conn, null, null);
        }
    }
} 